public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> t;
        queue<TreeNode*> p;
        p.push(root);
        int m,n;
        if (root == null) 
        {
            return t;
        }
        while (!p.empty())
        {
            n = p.size();
            m = INT_MIN;               
            while (n-- )
            {
                TreeNode* x = p.front();
                p.pop();
                if (x->left) p.push(x->left);
                if (x->right) p.push(x->right);
 
                m = max(m, x->val);   
            }
 
            t.push_back(m);              
        }
        return t;
    }
};